////////////////////////////////////////////////////////////////////////////////////////////////////
// file:	D:\dev\wanton\Wanton\source\scripting\scriptengine.h
//
// summary:	Declares the wanton class
//-----------------------------------------------------------------------------
// Copyright (c) 2013 Ash Matheson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////

#include "pch.h"
#include "ScriptEngine.h"
#include <iostream>
#include <assert.h>
#include "add_on\scripthandle\scripthandle.h"
#include "add_on\scriptstdstring\scriptstdstring.h"
#include "add_on\scriptbuilder\scriptbuilder.h"

using namespace std;

ScriptEngine::ScriptEngine(void) :
	m_engine(nullptr),
	m_hasCompileErrors(false)
{
}

ScriptEngine::~ScriptEngine(void)
{
	if (m_engine)
	{
		m_engine->Release();
	}
}

bool ScriptEngine::Initialize()
{
	int r;
	m_engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

	r = m_engine->SetMessageCallback(asMETHOD(ScriptEngine, MessageCallback), this, asCALL_THISCALL);

	RegisterStdString(m_engine);
	RegisterScriptHandle(m_engine);

	return m_engine != nullptr;
}

void ScriptEngine::MessageCallback(const asSMessageInfo &msg)
{
	const char *type = "ERR ";
	if( msg.type == asMSGTYPE_WARNING ) 
	{
		type = "WARN";
	}
	else if( msg.type == asMSGTYPE_INFORMATION ) 
	{
		type = "INFO";
	}

	cout << msg.section << " (" << msg.row << ", " << msg.col << ") : " << type << " : " << msg.message << endl;

	if( msg.type == asMSGTYPE_ERROR )
	{
		m_hasCompileErrors = true;
	}
}